package com.example.admin.task;

import com.example.admin.feign.ArticleFeign;
import com.example.admin.feign.WmNewFeign;
import com.example.admin.service.AdChannelService;
import com.example.admin.service.WmNewsService;
import com.hankcs.hanlp.seg.common.ResultTerm;
import com.usian.model.media.dtos.WmNewsDtos;
import com.usian.model.media.pojos.WmNews;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

@Component
@Slf4j
public class wemedisAutoJob {
    @Autowired
    private WmNewFeign wmNewFeign;
    @Autowired
    private ArticleFeign articleFeign;
    @Autowired
    private AdChannelService adChannelService;

    @XxlJob("wemedisAutoJob")
    public ReturnT<String> wemedisAutoJob(String message) {
        log.info("定时任务已经执行了");
        List<WmNews> checkedWmnews = wmNewFeign.getCheckedWmnews();
        if (!CollectionUtils.isEmpty(checkedWmnews)) {
            for (WmNews checkedWmnew : checkedWmnews) {
                log.info("发布的文章编号为:{}", checkedWmnew.getId());
                String channel = adChannelService.getAdmin(checkedWmnew.getChannelId());
                log.info(">>>>>>>>>>>>>>>>>>>>" + channel);
                WmNewsDtos wmNewsDtos = new WmNewsDtos();
                BeanUtils.copyProperties(checkedWmnew, wmNewsDtos);
                wmNewsDtos.setChannelName(channel);
                String name = wmNewFeign.selectName(checkedWmnew.getUserId());
                wmNewsDtos.setName(name);
                Integer integer = articleFeign.addArticle(wmNewsDtos);
                log.info(">>>>>>>>>>{}", integer);
                checkedWmnew.setArticleId(Long.parseLong(integer + ""));
                Integer integer1 = wmNewFeign.updateWmnews(checkedWmnew);
                log.info(">>>>>>>>>>>>>>>>{}", integer1);
                if (integer1 == 1) {
                    wmNewFeign.updateWmNews(checkedWmnew, 9);
                    log.info(">>>>>>>>>>>>执行成功");
                } else {
                    log.info(">>>>>>>>>>>执行失败");
                }
            }
        }
        return ReturnT.SUCCESS;
    }
}
